#ifndef PROTOCOL_07645_H
#define PROTOCOL_07645_H
#include <qglobal.h>//基本数据类型

#include <QDebug>
#include <QMessageBox>
#include <QString>
#include "comfunc.h"
#include "mainwindow.h"

#define DID645_LEN 4

#define FRAME_645_SZ        12

#define READ_DATA                   0x11       //读取数据
#define READ_FOLLOWING_DATA         0x12       //读取后续数据
//#define READ_DATA_AGAIN             0x03       //重读数据
#define WRITE_DATA                  0x14       //写数据
#define RADIATE_TIMING              0x08    //广播校时
#define WRITE_METER_ADDR            0x15      //写设备地址
#define READ_METER_ADDR             0x13      //读设备地址
#define SET_COMM_VELOCITY           0x17      //设置通信速率
#define SET_PASSWORD                0x18      //修改密码
//#define MAX_DEMAND_CLEAR            0x19      //需量清零
#define METER_CLEAR                 0x1A    //电表清零
#define EVENT_CLEAR                 0x1B      //事件清零
#define TRIP_CTRL                   0x1C      //闸控制
//#define OUTPUT_SWITCH               0x1E      //信号输出切换
#define RADIATE_FREEZE              0x16      //广播冻结
#define NORMAL_FREEZE               0x16      //普通冻结
//#define INTER_COMMAND               0x12      //用于校表
#define EVENT_REPORT                0x1F      //事件上报
#define EVENT_REPORT_REQUEST                0x9F      //事件上报返回确认






struct frame07645
{
    uint8_t st1;//头68H
    uint8_t meter_addr[6];
    uint8_t st2;//头68H

    union
    {
        uint8 control_byte;
        struct
        {
          uint8 function_flag:5;
          uint8 following_flag:1;//=1  后续数据
          uint8 exception_flag:1;//0正常/1异常
          uint8 direction_flag:1;//0下发/1响应
        };
    };
    uint8_t data_len;
    uint8_t data[2];//其实可以声明为data[1]，但是只要程序中出现了调用data[1+var]就会被编译器判定为越界警告，干脆声明为2得了
};
#define FRAME_645_HEAD      10 //从头到长度位置（含长度）

typedef struct _data645
{
    uint8 did[DID645_LEN];
    uint8 body[1];
}data645;

typedef struct _data_645write
{
    uint8 did[DID645_LEN];
    uint8 password[4];//密码
    uint8 manipulator[4];//操作者
    uint8 body[1];//数据体
}data_645write;//645写数据时的data域结构


struct FuncCodeAnaly
{
    uint8 func_code;
    const char *instruction;
};

//#define func_code_count 12
//extern struct FuncCodeAnaly func_code_analy[func_code_count];
struct frame07645 *get_645_frame(uint8_t *in, int len);
void get_645_did_analysis(QString did_str, QString &DidAnalyStr);
void analysis_07645_frame(struct frame07645 *pframe, QString &AnalyStr);

#endif // PROTOCOL_07645_H
